package cn.wolfcode.demo;


import org.activiti.engine.*;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.Test;

import java.util.HashMap;
import java.util.List;

public class demo4 {
    @Test
    public void testDemo(){
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        System.out.println(processEngine);
    }
    @Test
    public void testDeploy(){
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        RepositoryService repositoryService = processEngine.getRepositoryService();
        Deployment deployment = repositoryService.createDeployment().addClasspathResource("bpnm/leave-employee.bpmn")
                .addClasspathResource("bpnm/leave-employee.png")
                .name("员工请假流程").deploy();
        System.out.println(deployment.getId() + "流程部署ID");
        System.out.println("流程部署名称" + deployment.getName());
    }
    @Test
    public void testRun(){
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        RuntimeService runtimeService = processEngine.getRuntimeService();

        HashMap<String, Object> map = new HashMap<>();
        map.put("day","3");

        ProcessInstance instance = runtimeService.startProcessInstanceByKey("leave-employee",map);
        System.out.println("流程定义的id" + instance.getProcessDefinitionId());
        System.out.println("流程实例的id" + instance.getId());
    }
@Test
    public void testSelectTodoTaskList(){
    ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
    TaskService taskService = processEngine.getTaskService();
    List<Task> list = taskService.createTaskQuery().processDefinitionKey("leave-employee")
            .taskAssignee("lisi")
            .list();
    for(Task task:list){
        System.out.println("流程定义id = " + task.getProcessDefinitionId());
        System.out.println("流程实例id = " + task.getProcessInstanceId());
        System.out.println("任务id = " + task.getId());
        System.out.println("任务名称 = " + task.getName());
    }
}
@Test
    public void testCompleteTask(){
    ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
    TaskService taskService = processEngine.getTaskService();
    List<Task> list = taskService.createTaskQuery().processDefinitionKey("leave-employee").taskAssignee("lisi").list();
    for (Task task : list) {
        taskService.complete(task.getId());
    }
}
}
